package features.advance.jdk.math;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author LIN
 * @date 2021-11-06 15:20
 */
public class BigDecimalDemo {

    public static void main(String[] args) {

        BigDecimal bigDecimal = new BigDecimal("1.1100");
        BigDecimal bigDecimal1 = new BigDecimal("1.0");

        BigDecimal bigDecimal2 = BigDecimal.valueOf(999L, 2);
        System.out.println(bigDecimal2);

        System.out.println(bigDecimal.hashCode());
        System.out.println(bigDecimal1.hashCode());

        System.out.println(bigDecimal.compareTo(bigDecimal1));
        BigDecimalDemo demo = new BigDecimalDemo();

        demo.matchScale(new BigDecimal[]{bigDecimal,bigDecimal1});
        System.out.println("matchScale");
        System.out.println(bigDecimal.toPlainString());
        System.out.println(bigDecimal1.toPlainString());

        demo.precision(bigDecimal);
        demo.precision(bigDecimal1);
        demo.precision(new BigDecimal(".2"));
        demo.precision(new BigDecimal("0.222"));
        demo.precision(new BigDecimal("12345678901234567"));
    }

    public void matchScale(BigDecimal[] val){
        if (val[0].scale() == val[1].scale()) {
            return;
        } else if (val[0].scale() < val[1].scale()) {
            val[0] = val[0].setScale(val[1].scale(), RoundingMode.UNNECESSARY);
        } else if (val[1].scale() < val[0].scale()) {
            val[1] = val[1].setScale(val[0].scale(), RoundingMode.UNNECESSARY);
        }
    }

    public void precision(BigDecimal val){
        System.out.println("The precision of "+val.toPlainString()+" is "+val.precision());
    }

}
